package com.example.administrator.trains;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class myorder extends Activity {
    String user_id;

    List<Map<String,String>> list2 = new ArrayList<Map<String, String>>();
    List<Map<String,String>> list = new ArrayList<Map<String, String>>();

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==1){
                ListView lv = (ListView) findViewById(R.id.lv2);
                for (int i = 0;i < list2.size();i++) {
                    ListAdapter la = new SimpleAdapter(myorder.this,list2,
                            R.layout.myorder_list_item,
                            new String[]{"my_name","identityCard",
                                    "phone","startPlace",
                                    "endPlace","startTime",
                                    "endTime","date",
                                    "price2", "type"},
                            new int[]{R.id.name,R.id.identify,
                                    R.id.phone,R.id.start,
                                    R.id.end,R.id.start_time,
                                    R.id.end_time,R.id.date,
                                    R.id.price, R.id.type});
                    lv.setAdapter(la);
                }

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent i = new Intent(myorder.this,administrate_myorder.class);

                        //将数据传给下一个活动
                        i.putExtra("date",list.get(position).get("date").toString());
                        i.putExtra("startPlace",list.get(position).get("startPlace").toString());
                        i.putExtra("endPlace",list.get(position).get("endPlace").toString());
                        i.putExtra("startTime",list.get(position).get("startTime").toString());
                        i.putExtra("endTime",list.get(position).get("endTime").toString());
                        i.putExtra("orderNum",list.get(position).get("orderNum").toString());
                        i.putExtra("type",list.get(position).get("type").toString());
                        i.putExtra("id", user_id);
                        i.putExtra("price",list.get(position).get("price").toString());


                        startActivity(i);
                    }
                });
            }
            if(msg.what==2){
                Toast.makeText(myorder.this, "服务器没有响应，请重试", Toast.LENGTH_LONG).show();
                return;
            }
            if(msg.what==3){
                Toast.makeText(myorder.this, "还未购买车票", Toast.LENGTH_LONG).show();
                return;
            }
        }
    };

    private byte one_data;
    private int k = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_myorder);

        Intent intent = getIntent();
        user_id = intent.getStringExtra("id");

        new Thread(){
            @Override
            public void run() {
                super.run();
                byte[] buffer = new byte[20000];
                int j = 1;
                byte[] one_ticket = new byte[300];
                byte[] from_station = new byte[30];
                byte[] to_station = new byte[30];
                byte[] train_number = new byte[10];
                byte[] departure_time = new byte[10];
                byte[] arrival_time = new byte[10];
                byte[] to_date = new byte[11];
                byte[] price = new byte[5];
                byte[] identity = new byte[5];
                byte[] phone_num = new byte[5];
                byte[] my_name = new byte[5];
                byte[] ticket_type = new byte[20];
                byte no_ticket = 7;

                try {
                    Socket.connector.send(Socket.connector.package_data(Socket.connector.TICKET_CMD_TYPE,
                            Socket.connector.FIND_ORDER, "00", user_id.trim() + CommunicationData.empty));
                    /*接收服务端是否成功接收信息的ack*/
                    Socket.connector.recv_ack();
                    if (CommunicationData.ack == "01") {
                        Socket.connector.rec_data(buffer);
                        if (buffer[0] == no_ticket){
                            Socket.connector.send(Socket.connector.hexStringToByteArray(Socket.connector.ACK_OK));
                            Message m = new Message();
                            m.what=3;
                            myHandler.sendMessage(m);
                            return;
                        }
                        Socket.connector.send(Socket.connector.hexStringToByteArray(Socket.connector.ACK_OK));
                    } else {
                        Message m = new Message();
                        m.what=2;
                        myHandler.sendMessage(m);

                        return;
                    }
                } catch (IOException e) {
                }

                for (int i = 7; i > 0; i++) {
                    one_data = buffer[i];
                    if (one_data == '|') {
                        switch (j) {
                            case 1:
                                from_station = one_ticket.clone();
                                break;
                            case 2:
                                to_station = one_ticket.clone();
                                break;
                            case 3:
                                departure_time = one_ticket.clone();
                                break;
                            case 4:
                                arrival_time = one_ticket.clone();
                                break;
                            case 5:
                                train_number = one_ticket.clone();
                                break;
                            case 6:
                                price = one_ticket.clone();
                                break;
                            case 7:
                                to_date = one_ticket.clone();
                                break;
                            case 8:
                                identity = one_ticket.clone();
                                break;
                            case 9:
                                my_name = one_ticket.clone();
                                break;
                            case 10:
                                phone_num = one_ticket.clone();
                                break;
                            case 11:
                                ticket_type = one_ticket.clone();
                                break;
                        }
                        while (k >= 0) {
                            one_ticket[k] = 0;
                            k--;
                        }
                        k = 0;
                        j++;
                        continue;
                    }
                    /*读完一条车票信息，加入map中*/
                    if (one_data == ',') {
                        String str_train_number = new String(train_number).trim();
                        String str_from_station = new String(from_station).trim();
                        String str_to_station = new String(to_station).trim();
                        String str_departure_time = new String(departure_time).trim();
                        String str_arrival_time = new String(arrival_time).trim();
                        String str_to_data = new String(to_date).trim();
                        String str_price = new String(price).trim();
                        String str_my_name = new String(my_name).trim();
                        String str_identity = new String(identity).trim();
                        String str_phone_num = new String(phone_num).trim();
                        String str_ticket_type = new String(ticket_type).trim();

                        Map<String, String> map = new HashMap<String, String>();
                        map.put("date", str_to_data);
                        map.put("endPlace", str_to_station);
                        map.put("identityCard", str_identity);
                        map.put("my_name", str_my_name);
                        map.put("phone", str_phone_num);
                        map.put("startPlace", str_from_station);
                        map.put("price", str_price);
                        map.put("price2", str_price+"元");
                        map.put("startTime", str_departure_time);
                        map.put("endTime", str_arrival_time);
                        map.put("type", str_ticket_type);
                        list2.add(map);

                        Map<String, String> map2 = new HashMap<String, String>();
                        map2.put("date", str_to_data);
                        map2.put("endPlace", str_to_station);
                        map2.put("departure_time", str_departure_time);
                        map.put("orderNum", str_train_number);
                        map2.put("arrival_time", str_arrival_time);
                        map2.put("startPlace", str_from_station);
                        map.put("type", str_ticket_type);
                        list.add(map);

                        k = 0;
                        j = 1;
                        continue;
                    }
                    if (one_data == '.') {
                        break;
                    }
                    one_ticket[k] = one_data;
                    k++;
                }

                Message m = new Message();
                m.what=1;
                myHandler.sendMessage(m);
            }
        }.start();
    }
}
